import pygame

class Timer:
    def __init__(self,duration,func=None):
        #持续时间
        self.duration=duration
        self.func=func
        self.start_time=0
        self.active=False
    #开始
    def activate(self):
        self.active=True
        self.start_time=pygame.time.get_ticks()
    #结束
    def deactivate(self):
        self.active=False
        self.start_time=0

    def update(self):
        current_time = pygame.time.get_ticks()  # 获取当前时间
        if self.active and current_time - self.start_time >= self.duration:  # 计算持续时间
            if self.func and self.start_time != 0:
                self.func()
            self.deactivate()